global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/8_new_hires", replace


*Loop over 36 months
cap program drop loop
program define loop 
args year month ref end
clear all 
	
	forvalues i=0/36 {
	    local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
				
			*Calculate flows from last month
				capture use yr_month state client_id emp_pur_c salaried base ot gross male naics using "$temp\cleaned_`y'`m'", clear
				capture use yr_month state client_id emp_pur_c salaried base ot gross male naics using "$temp\cleaned_`y'0`m'", clear
				
				*Rename and merge on previous month
				rename salaried salaried_post
				rename base base_post
				rename ot ot_post
				local m2=mod(`month'-2+`i',12)+1
				local y2=`year'+int((`month'-2+`i')/12)
				if `m2'<10 {
				merge 1:1 client_id state emp_pur_c using "$temp\cleaned_`y2'0`m2'", keepusing(client_id state emp_pur_c salaried base ot)
				}
				if `m2'>=10 {
				merge 1:1 client_id state emp_pur_c using "$temp\cleaned_`y2'`m2'", keepusing(client_id state emp_pur_c salaried base ot)
				}
				
				*Define flows
				gen hires=_merge==1
				gen sep=_merge==2
				gen stay=_merge==3 & salaried==salaried_post 
				gen reclass=_merge==3 & salaried!=salaried_post
				rename _merge merge_flows
				
				*Merge on balanced firms and numeric client_id
				merge m:1 client_id using "$output/balanced_firms_`ref'"
				keep if _merge==3
				drop _merge
				
				*Create bins
				gen bin=floor((base_post-`end')/40)*40
				gen bin_pre=floor((base-`end')/40)*40
					
			*Save new hires
			preserve 
				keep if hires==1
				drop salaried base ot
				rename salaried_post salaried
				rename base_post base 
				rename ot_post ot
				keep yr_month state client_id emp_pur_c salaried base ot bin gross male naics
				compress
				save "$temp\hires_`y'`m'", replace
			restore
			
			*Save wage changers
			preserve 
				keep if stay==1 & base!=base_post 
				*replace yr_month=mdy(`m2',01,`y2')
				keep yr_month state client_id emp_pur_c salaried base* ot* bin* male naics
				compress
				save "$temp\wage_changers_`y'`m'", replace
			restore
	}
	
	*Append hires
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\hires_`y'`m'"
	}		
	compress
	save "$temp\new_hires_`ref'", replace
	
	*Append wage changers
	clear all 
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
	    append using "$temp\wage_changers_`y'`m'"
	}		
	compress
	save "$temp\wage_changers_`ref'", replace
	
	*Delete temp files
	forvalues i=0/36 {
		local m=mod(`month'-1+`i',12)+1
		local y=`year'+int((`month'-1+`i')/12)
		erase "$temp\hires_`y'`m'.dta"
		erase "$temp\wage_changers_`y'`m'.dta"
	}
end 

loop 2015 5 treat 913
loop 2013 5 control 913

log close